﻿using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using System.Xml;
using JQuery.WebKit.Common.Caching;

namespace JQuery.WebKit.WebCommon.Theming
{
    public class ThemeManager
    {
        public static List<Theme> GetThemes()
        {
            return GetThemes(true);
        }

        public static List<Theme> GetThemes(bool includeHidden)
        {
            DirectoryInfo dInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath("App_Themes"));

            DirectoryInfo[] dArrInfo = dInfo.GetDirectories();
            List<Theme> list = new List<Theme>();
            foreach (DirectoryInfo sDirectory in dArrInfo)
            {
                Theme theme = GetTheme(sDirectory.Name, sDirectory.FullName, includeHidden);
                if (theme == null)
                {
                    continue;
                }
                list.Add(theme);
            }
            return list;
        }

        private static Theme GetTheme(string name, string path, bool includeHidden)
        {
            Theme theme = (Theme)Cache.Current.GetItem(string.Format("AppTheme_{0}", name));
            if (theme != null)
            {
                return theme.Visible || includeHidden ? theme : null;
            }
            string xml = Path.Combine(path, "theme.xml");
            if (!File.Exists(xml))
            {
                return null;
            }
            theme = new Theme(name);
            using (XmlDataSource dataSource = new XmlDataSource())
            {
                dataSource.DataFile = xml;
                XmlDocument xdoc = dataSource.GetXmlDocument();
                XmlElement root = xdoc.DocumentElement;

                foreach (XmlNode rt in root.ChildNodes)
                {
                    if (rt.NodeType != XmlNodeType.Element)
                    {
                        continue;
                    }
                    switch (rt.Name)
                    {
                        case "name":
                            theme.DisplayName = rt.InnerText;
                            break;
                        case "description":
                            theme.Description = rt.InnerText;
                            break;
                        case "preview":
                            theme.PreviewImagePath = string.Join("/", "~/App_Themes", name, rt.InnerText);
                            break;
                        case "default":
                            bool def = false;
                            if (bool.TryParse(rt.InnerText, out def))
                            {
                                theme.IsDefaultTheme = def;
                            }
                            break;
                        case "visible":
                            bool visible;
                            if (bool.TryParse(rt.InnerText, out visible))
                            {
                                theme.Visible = visible;
                            }
                            break;
                    }
                }
            }
            Cache.Current.AddItem(string.Format("AppTheme_{0}", name), theme);
            return theme.Visible || includeHidden ? theme : null;
        }

        public static Theme GetDefaultTheme()
        {
            Theme theme = (Theme)Cache.Current.GetItem("AppDefaultTheme");
            if (theme != null)
            {
                return theme;
            }
            DirectoryInfo dInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath("App_Themes"));
            List<Theme> list = new List<Theme>();

            if (dInfo.Exists)
            {
                DirectoryInfo[] dArrInfo = dInfo.GetDirectories();
                foreach (DirectoryInfo sDirectory in dArrInfo)
                {
                    theme = GetTheme(sDirectory.Name, sDirectory.FullName, true);
                    if (theme.IsDefaultTheme)
                    {
                        return theme;
                    }
                    list.Add(theme);
                }
            }
            if (list.Count > 0)
            {
                Cache.Current.AddItem("AppDefaultTheme", list[0]);
                return list[0];
            }
            return null;
        }

    }
}
